package com.zhuge_new.Activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.CompoundButton
import android.widget.Switch
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.maning.mndialoglibrary.MProgressDialog
import com.zhuge_new.Base.BaseActivity
import com.zhuge_new.Base.getString
import com.zhuge_new.Base.toast
import com.zhuge_new.R
import com.zhuge_new.http.StringDialogCallback
import com.zhuge_new.share.BaseHttp
import com.zhuge_new.untils.GlideCacheUtil
import kotlinx.android.synthetic.main.activity_set.*
import kotlinx.android.synthetic.main.layout_title.*
import org.json.JSONObject
import com.flyco.dialog.listener.OnBtnClickL
import android.graphics.Color.parseColor
import android.util.Log
import android.view.Gravity
import com.flyco.dialog.widget.NormalDialog
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.zhuge_new.MainActivity
import com.zhuge_new.untils.ActivityStack


/**
 * Created by lenovo on 2018-09-11.
 */
class SetActivity : BaseActivity(), View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private lateinit var mMProgressDialog: MProgressDialog
    private lateinit var dialog: NormalDialog

    private var handler = Handler(object : Handler.Callback {
        override fun handleMessage(msg: Message): Boolean {
            when (msg.what) {
                0 -> mMProgressDialog.dismiss()
            }
            return false
        }
    })

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set)
        init_title()
    }

    override fun init_title() {
        super.init_title()
        val intent = intent
        val pass = intent.getStringExtra("pass")
        tv_nav_title.text = "设置"
        Swich.isChecked = !pass.equals("1")
        iv_nav_back.setOnClickListener(this)
        Swich.setOnCheckedChangeListener(this)
        set_password.setOnClickListener(this)
        feed_back.setOnClickListener(this)
        clear_cache.setOnClickListener(this)
        setting_quit.setOnClickListener(this)
        if (ActivityStack.getScreenManager().isContainsActivity(LoginActivity::class.java)) {

        } else {
            ActivityStack.getScreenManager().pushActivity(this)
        }

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_nav_back -> {
                finish()
            }
            R.id.set_password -> {
                val intent = Intent(this@SetActivity, PasswordActivity::class.java)
                startActivity(intent)
            }
            R.id.feed_back -> {
                val intent = Intent(this@SetActivity, FeedBackActivity::class.java)
                startActivity(intent)
            }
            R.id.clear_cache -> {
                GlideCacheUtil.getInstance().clearImageMemoryCache(this)
                mMProgressDialog = MProgressDialog.Builder(baseContext)
                        .setCancelable(true)
                        .isCanceledOnTouchOutside(false)
                        .setDimAmount(0.5f)
                        .build()
                mMProgressDialog.show()
                handler.sendEmptyMessageDelayed(0, 2000)
            }
            R.id.setting_quit -> {
                quit_current()
            }
        }
    }


    override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
        when (buttonView.id) {
            R.id.Swich ->
                if (isChecked) {
                    isPrivacy(1)
                } else {
                    isPrivacy(0)
                }
        }

    }

    private fun isPrivacy(isOpen: Int) {
        OkGo.post<String>(BaseHttp.privacy_set)
                .tag(this@SetActivity)
                .headers("token", getString("token"))
                .params("isOpen", isOpen)
                .execute(object : StringDialogCallback(baseContext) {
                    override fun onSuccessResponse(response: Response<String>, msg: String, msgCode: String) {
                        println(response.message())
                        if (msg == "修改成功") {
                            toast("msg")
                        } else {
                            toast(getString(R.string.modify_fail))
                        }
                    }
                })
    }

    private fun quit_current() {
        dialog = NormalDialog(this)
        dialog.isTitleShow(false)
                .bgColor(Color.parseColor("#383838"))
                .cornerRadius(5f)
                .content("确定退出当前账号吗?")
                .contentGravity(Gravity.CENTER)
                .contentTextColor(Color.parseColor("#ffffff"))
                .dividerColor(Color.parseColor("#222222"))
                .btnTextSize(15.5f, 15.5f)
                .btnTextColor(Color.parseColor("#ffffff"), Color.parseColor("#ffffff"))//
                .btnPressColor(Color.parseColor("#2B2B2B"))//
                .widthScale(0.85f)
        dialog.show()

        dialog.setOnBtnClickL(
                OnBtnClickL {
                    dialog.dismiss()
                },
                OnBtnClickL {
                    dialog.dismiss()
                    val intent = Intent(this@SetActivity, LoginActivity::class.java)
                    ActivityStack.getScreenManager().popOneActivity(SetActivity::class.java)
                    startActivity(intent)
                })
    }

    override fun onPause() {
        super.onPause()
    }


}

